﻿using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
using Yoozoo.UI.YGUI;

namespace Yoozoo.Gameplay.YGUICustom
{
    /// <summary>
    /// 倒计时文本组件
    /// 该组件只有每秒会进行倒计时更新的文本才使用
    /// </summary>
    public class YCountdownComponent : MonoBehaviour
    {
        private enum ECountdownShowType
        {
            NotDateTime = 1, // 非日期时间
            DateTime,        // 日期时间
            DateTimeNotDay,  // 日期时间不显示天
        }

        public YText CountdownText;
        private int m_Seconds;
        private int m_CountdownShowType;
        private string m_CountdownTextContent;
        private string m_DateTimeTextContent;

        /// <summary>
        /// 设置倒计时文本信息
        /// </summary>
        /// <param name="seconds">秒数</param>
        /// <param name="countdownShowType">对应ECountdownShowType</param>
        /// <param name="countdownTextContent">倒计时文本内容，用于拼接(没有则不传)</param>
        /// <param name="dateTimeTextContent">如果是日期时间格式类型的倒计时，天数大于0要显示天数，就需要传一个用于拼接的文本进来</param>
        public void SetCountdownSeconds(int seconds, int countdownShowType, string countdownTextContent = null, string dateTimeTextContent = null)
        {
            this.m_Seconds = seconds;
            this.m_CountdownShowType = countdownShowType;
            this.m_CountdownTextContent = countdownTextContent;
            this.m_DateTimeTextContent = dateTimeTextContent;
            RefreshCountdown();
        }

        public void RefreshCountdown()
        {
            m_Seconds--;
            if (CountdownText != null)
            {
                CountdownText.text = GetCountdownConvertText();
            }
        }

        private string GetCountdownConvertText()
        {
            string convertText = string.Empty;
            if (m_CountdownShowType == (int)ECountdownShowType.NotDateTime)
            {
                if (m_CountdownTextContent == null)
                {
                    convertText = m_Seconds.ToString();
                }
                else
                {
                    convertText = string.Format(m_CountdownTextContent, convertText);
                }
            }
            else
            {
                TimeSpan timeSpan = new TimeSpan(0, 0, m_Seconds);
                if (m_CountdownShowType == (int)ECountdownShowType.DateTime)
                {
                    if (timeSpan.Days > 0)
                    {
                        if (m_CountdownTextContent == null)
                        {
                            convertText = string.Format(m_DateTimeTextContent, timeSpan.Days, timeSpan.ToString(@"hh\:mm\:ss"));
                        }
                        else
                        {
                            convertText = string.Format(m_CountdownTextContent, string.Format(m_DateTimeTextContent, timeSpan.Days, timeSpan.ToString(@"hh\:mm\:ss")));
                        }
                    }
                    else
                    {
                        if (m_CountdownTextContent == null)
                        {
                            convertText = timeSpan.ToString(@"hh\:mm\:ss");
                        }
                        else
                        {
                            convertText = string.Format(m_CountdownTextContent, timeSpan.ToString(@"hh\:mm\:ss"));
                        }
                    }
                }
                else if (m_CountdownShowType == (int)ECountdownShowType.DateTimeNotDay)
                {
                    if (m_CountdownTextContent == null)
                    {
                        convertText = timeSpan.ToString(@"hh\:mm\:ss");
                    }
                    else
                    {
                        convertText = string.Format(m_CountdownTextContent, timeSpan.ToString(@"hh\:mm\:ss"));
                    }
                }
            }
            return convertText;
        }
    }
}
